package com.local;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * 使用.class方式调用不会触发类的初始化，而是延迟到第一次调用静态方法或者【静态非常量成员】时触发
 * 而Class.forName()这种则会触发类的初始化操作
 * @author relieved-gao
 * @version 1.0 , 2017/11/14
 * @since 1.0
 */
public class RTTItest {

	public static void main(String[] args) throws FileNotFoundException {
		InputStream in = new FileInputStream(new File("/"));
		Class test1 = Test1.class;
		System.out.println("After creating Test1 ref=====================================");
		System.out.println(Test1.staticFiled);
		System.out.println(Test1.staticFiledRand);
		System.out.println("=======================================================");
		System.out.println(Test2.staticFiled);
		Class test3 = null;
		try {
			test3 = Class.forName("com.local.Test3");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		System.out.println("After creating Test3 ref=====================================");
		System.out.println(Test3.staticFiled);

	}

}

class Test1 {
	static final int staticFiled = 50;
	static final int staticFiledRand = (int) Math.random() * 1000;

	static {
		System.out.println("testInit=========================================Test1");
	}
}

class Test2 {
	static int staticFiled = 20;

	static {
		System.out.println("testInit=========================================Test2");
	}
}

class Test3 {
	static int staticFiled = 10;

	static {
		System.out.println("testInit=========================================Test3");
	}
}
